package com.hyd.manage.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.hyd.common.utils.DateUtils;
import com.hyd.manage.domain.Medicines;
import com.hyd.manage.domain.SalesRecord;
import com.hyd.manage.service.IMedicinesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hyd.common.annotation.Log;
import com.hyd.common.core.controller.BaseController;
import com.hyd.common.core.domain.AjaxResult;
import com.hyd.common.enums.BusinessType;
import com.hyd.manage.domain.Sales;
import com.hyd.manage.service.ISalesService;
import com.hyd.common.utils.poi.ExcelUtil;
    import com.hyd.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * post销售Controller
 *
 * @author hwb
 * @date 2025-02-19
 */
@Api(tags = "post销售Controller")
@RestController
@RequestMapping("/manage/sales")
@Slf4j
public class SalesController extends BaseController
{
    @Autowired
    private ISalesService salesService;
    @Autowired
    private IMedicinesService medicinesService;

    /**
     * 查询post销售列表
     */
    @ApiOperation("查询post销售列表")
    @PreAuthorize("@ss.hasPermi('manage:sales:list')")
    @GetMapping("/list")
    public TableDataInfo list(Sales sales)
    {
        startPage();
        List<Sales> list = salesService.selectSalesList(sales);
        return getDataTable(list);
    }

    /**
     * 导出post销售列表
     */
    @ApiOperation("导出post销售列表")
    @PreAuthorize("@ss.hasPermi('manage:sales:export')")
    @Log(title = "post销售", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Sales sales)
    {
        List<Sales> list = salesService.selectSalesList(sales);
        ExcelUtil<Sales> util = new ExcelUtil<Sales>(Sales.class);
        util.exportExcel(response, list, "post销售数据");
    }

    /**
     * 获取post销售详细信息
     */
    @ApiOperation("获取post销售详细信息")
    @PreAuthorize("@ss.hasPermi('manage:sales:query')")
    @GetMapping(value = "/{medicineCode}")
    public TableDataInfo getInfo(@PathVariable("medicineCode") String medicineCode)
    {
        startPage();
        List<Medicines> list = medicinesService.selectMedicinesListByCode(medicineCode);
        return getDataTable(list);
    }

    /**
     * 新增post销售
     */
    @ApiOperation("新增post销售")
    @PreAuthorize("@ss.hasPermi('manage:sales:add')")
    @Log(title = "post销售", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional
    public AjaxResult add(@RequestBody List<Sales> salesList)
    {
        for (Sales sales : salesList) {
            log.info("sales:{}", sales);
            //判断数量是否为空
            if (sales.getQuantity() == null) {
                continue;
            }
            //计算总金额
            sales.setTotalAmount(sales.getSalePrice().multiply(BigDecimal.valueOf(sales.getQuantity())));
            //设置销售日期
            sales.setSaleDate(DateUtils.getNowDate());
            //减少对应库存
            medicinesService.updateStockQuantity(sales.getMedicineCode(), sales.getBatchNumber(), sales.getQuantity());
            //保存到数据库
            salesService.insertSales(sales);
        }
        /*//计算总金额
        sales.setTotalAmount(sales.getSalePrice().multiply(BigDecimal.valueOf(sales.getQuantity())));
        //设置销售日期
        sales.setSaleDate(DateUtils.getNowDate());*/
        //return toAjax(salesService.insertSales(sales));
        return AjaxResult.success();
    }

    @ApiOperation("收款")
    @PreAuthorize("@ss.hasPermi('manage:sales:edit')")
    @Log(title = "收款", businessType = BusinessType.UPDATE)
    @PostMapping("/receipt")
    public AjaxResult receipt(@RequestBody SalesRecord salesRecord) {
        return toAjax(salesService.receipt(salesRecord));
    }

    /**
     * 修改post销售
     */
    @ApiOperation("修改post销售")
    @PreAuthorize("@ss.hasPermi('manage:sales:edit')")
    @Log(title = "post销售", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Sales sales)
    {
        return toAjax(salesService.updateSales(sales));
    }

    /**
     * 删除post销售
     */
    @ApiOperation("删除post销售")
    @PreAuthorize("@ss.hasPermi('manage:sales:remove')")
    @Log(title = "post销售", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(salesService.deleteSalesByIds(ids));
    }

    @ApiOperation("挂单")
    @PreAuthorize("@ss.hasPermi('manage:sales:edit')")
    @Log(title = "挂单", businessType = BusinessType.UPDATE)
    @PostMapping("/{ids}")
    public AjaxResult hangSales(@PathVariable Long[] ids) {
        return toAjax(salesService.hangSales(ids));
    }
}